import { login } from '@/api/user'
import { getUserInfo, setUserInfo, removeUserInfo } from '@/utils/auth'

const getDefaultState = () => {
  return {
    userInfo: getUserInfo() || {}
  }
}

const state = getDefaultState()

const mutations = {
  setUser(state, payload) {
    state.userInfo = payload
    setUserInfo(payload)
  },
  removeUser(state) {
    state.userInfo = {}
    removeUserInfo()
  }
}

const actions = {
  async loginUser(state, payload) {
    const data = await login(payload)
    state.commit('setUser', data)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

